
jQuery(document).ready(function() {
	var repositoryPK = getUrlParam('repositoryPK');
	initForm(repositoryPK);
	//列表查询事件
//	$("#bigClass").change(function(){
//		
//		var selectBig = $("#bigClass").val();
//		if(selectBig){
//			bindDictForSmallClass("smallClass","REPOSITORY_XL","",selectBig);
//		}else{
//			$('#smallClass').empty(); 
//		}
//		
//	});

})

function initForm(repositoryPK){

	var infoUrl = projectPath + "repository/list.do";
	
	$.ajax({ 
		url: infoUrl,
		method: 'post',
		data:JSON.stringify({"repositoryPK": repositoryPK}),
		dataType: "json",
		contentType: "application/json",
		success: function(data){ 
			bindFormRepositoryInfo(data.rows[0]);
      	}
	});
}

function bindFormRepositoryInfo(data){
	$('[name="repositoryPK"]').val(data.repositoryPK);
	$('[name="repositoryCode"]').val(data.repositoryCode);
	$('[name="repositoryTitle"]').val(data.repositoryTitle);
	
	//处理关键字
	var keyword = data.keyWord;
	var keywordArr = keyword.split(",");
	$.each(keywordArr, function(index, item) {
		$('[name="keyWord"]').tagsinput('add',item);
    })
    
    //处理大类
    bindDict("bigClass","DM_XKXMLX", data.bigClass);
	$('[name="detail"]').val(data.detail);
	$('[name="releaseTime"]').val(data.releaseTime);
	$('[name="issuer"]').val(data.issuer);
	$('[name="viewCount"]').val(data.viewCount);
	
}

function save(){
	var repositoryData = $("#repositoryForm").serializeObject();
	if(!check(repositoryData))
		return true;
	
	$.ajax({ 
		method: 'post',
		url: basePath() + 'repository/update.do',
		data: JSON.stringify(repositoryData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
			window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success, {
				onOk: function(){
					if(data.result == 1){
						parent.refreshData();//刷新数据
					}
				}
			});	
      	}
		
	});
	
}

//ajax获取字典数据
function bindDictForSmallClass(selectID, dictcode, selectdata, bigClass){
	var infoUrl = projectPath + "dict/getDictDetail.do";
		$.ajax({ 
			url: infoUrl,
			data: {
				"dictcode": dictcode,
				"bigClass": bigClass
			},
			dataType: "json",
			success: function(data){
				if(data.result == 1){   
					var dataStr = data.datas;
					
					// 先清空控件数据
					$('#' + selectID).empty(); 

//					var option = $("<option>").val("").text(""); 
//					$('#' + selectID).append(option); 
					
					for (var i = 0; i < dataStr.length; i++) {
						var value = dataStr[i].itemcode;
						var text = dataStr[i].itemname;
						
						var option = $("<option>").val(value).text(text); 
						$('#' + selectID).append(option); 
					}	
					
					//选中下拉列表的值
					if(selectdata != ""){
						$('#' + selectID).val(selectdata);
//						$('#' + selectID).find("option[value="+selectdata+"]").prop("selected",true);
					}
				}else{
					$('#' + selectID).empty(); 
				}
	    	}
		});
	
}

function check(data){	
	var result = true;

	var mustStr = "repositoryTitle,keyWord,detail,bigClass,viewCount,issuer,releaseTime";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}

	

	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}

